package net.obxlabs.tweetpad.stats;

import java.awt.geom.*;
import net.silentlycrashing.util.*;
import penner.easing.*;

public class AnimatedPoint extends Point2D.Float {
	// starting point
	public float sX;
	public float sY;
	// offset point
	public float oX;
	public float oY;
	
	private boolean moving;
	private int animTime;
	private int animDuration;
	
	private RegisteredAction doneAction;
	
	public AnimatedPoint(float x, float y) {
		super(x, y);
		moving = false;
	}
	
	/**
     * Registers the given method with the done action.
     * 
     * @param name the name of the method to register
     * @param o the Object holding the method
     */
	public void registerDoneAction(String name, Object o) {
		doneAction = new RegisteredAction(name, o);
    }
	
	public void moveTo(float targetX, float targetY, int duration) {
		// set the starting point
		sX = x;
		sY = y;
		// set the target point
		oX= targetX-x;
		oY = targetY-y;
		// set the animation duration
		animDuration = duration;
		
		// tell the system to start animating
		animTime = 0;
		moving = true;
	}
	
	public void pre() {
		if (moving) {
			x = Expo.easeOut(animTime, sX, oX, animDuration);
			y = Expo.easeOut(animTime, sY, oY, animDuration);
			
			if (animTime < animDuration) {
                // the points are still moving, keep going
				animTime++;
            } else {
                // the animation is done, clean up
            	moving = false;
            	
            	// invoke the callback, if any
            	if (doneAction != null) {
            		doneAction.invoke();
            		doneAction = null;
            	}
            }
		}
	}
}
